﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Represents a Gallery control in the SharePoint Server Ribbon.
    /// </summary>
    public class RibbonGallery : RibbonVisualControl, IRibbonContainer<RibbonGalleryButton>
    {
        internal override string XmlTag
        {
            get { return Schema.Gallery; }
        }

        /// <summary>
        /// Child controls.
        /// </summary>
        public IEnumerable<RibbonGalleryButton> Controls { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public RibbonGalleryElementSize ElementDimensions = RibbonGalleryElementSize.Size48by48;

        private int _width = 10;

        ///<summary>
        /// 
        ///</summary>
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        internal IEnumerable<RibbonControl> AllControls()
        {
            var res = new List<RibbonControl>();
            var controlIndex = 0;
            foreach (var control in Controls
                .Where(c => c.Visible))
            {
                controlIndex++;
                control.NameSpace = FullId;
                control.Sequence = controlIndex;
                control.ElementDimensions = ElementDimensions;
                res.Add(control);
            }
            return res;
        }

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();
            element.Add(new XAttribute(Schema.ElementDimensions, ElementDimensions.ToString()));
            element.Add(new XAttribute(Schema.Width, Width));
            foreach (RibbonGalleryButton control in Controls)
            {
                control.NameSpace = FullId;
                control.ElementDimensions = ElementDimensions;
                element.Add(control.AsXElement());
            }
            return element;
        }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonGallery.
        /// </summary>
        public RibbonGallery(){}

        /// <summary>
        /// Creates and initializes a new instance of the RibbonGallery.
        /// </summary>
        /// <param name="id">Identifier</param>
        public RibbonGallery(string id):base(id){}

        /// <summary>
        /// Creates and initializes a new instance of the RibbonGallery.
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <param name="title">Title</param>
        public RibbonGallery(string id, string title): base(id, title){}
    }
}
